<template>
  <el-dialog
    title="角色分配"
    :visible.sync="visible"
    :before-close="handleClose"
    @open="resetNewCate"
  >
    <el-form :model="cate" :rules="$rules.cate" ref="cateRuleForm">
      <el-form-item
        label="分类名称"
        :label-width="formLabelWidth"
        prop="cat_name"
      >
        <el-input v-model="cate.cat_name"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleModifyCate">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  props: {
    modifyCategoriesVisible: {
      type: Boolean,
      default: false,
    },
    cateSelectForm: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      visible: false,
      formLabelWidth: "80px",
      cate: {
        cat_id: 0,
        cat_name: "",
      },
    };
  },
  methods: {
    async handleModifyCate() {
      await this.$refs.cateRuleForm.validate();
      if (this.cat_name == this.cateSelectForm.cat_name) return;
      let result = await this.$api.modifyCategoriesByCid(
        this.cate.cat_id,
        this.cate
      );
      console.log(result);

      this.$emit("handleModifyCate", false);
    },
    handleClose() {
      this.$emit("update:modifyCategoriesVisible", false);
    },
    resetNewCate() {
      this.cate.cat_name = this.cateSelectForm.cat_name;
      this.cate.cat_id = this.cateSelectForm.cat_id;
    },
  },
  watch: {
    modifyCategoriesVisible() {
      this.visible = this.modifyCategoriesVisible;
    },
  },
};
</script>